<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="课程代码">
              <a-input v-model="queryParam.kch"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="课程名称">
              <a-input v-model="queryParam.kcmc"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select :disabled="selectMenu.yx.length === 1" allowClear showSearch :filterOption="filterOption"
                          v-model="queryParam.kcssyxm">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

          </template>
          <a-col :md="6" :sm="12">
            <a-form-item
              label="审核时间"
            >
              <a-range-picker v-model="queryParam.auditTime"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons">
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="batchDelete">批量删除</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <p style="color:#EC0000">温馨提示：此处查询的结果是所有审核通过的课程信息</p>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :scroll="{ x: 1400 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="auditTime" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <template slot="operation" slot-scope="a,b">
          <a @click="showQuote(b)">被引</a>
          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="b.kkzt !== '02'">
                <a @click="triggerCourse(b, '02')">停用</a>
              </a-menu-item>
              <a-menu-item v-if="b.kkzt !== '01'">
                <a @click="triggerCourse(b, '01')">启用</a>
              </a-menu-item>
              <a-menu-item v-if="b.kkzt !== '01'">
                <a @click="deleteCourse(b)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </s-table>
    </a-card>

    <course-quote :visible="quoteVisible" :list="quoteList" @cancel="quoteVisible = false"/>
  </div>
</template>

<script>
import { STable } from '~'
import courseQuote from '../components/courseQuote'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '课程代码',
          dataIndex: 'kch'
        },
        {
          title: '课程名',
          dataIndex: 'kcmc'
        },
        {
          title: '英文名',
          dataIndex: 'kcywmc',
          width:260
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '课程层次',
          dataIndex: 'kcccmc'
        },
        {
          title: '开课状态',
          dataIndex: 'kkztc'
        },
        {
          title: '审核时间',
          dataIndex: 'auditDate',
          scopedSlots: {customRender: 'auditTime'}
        },
        {
          title: '操作',
          width: '140px',
          dataIndex: 'operation',
          fixed: 'right',
          scopedSlots: {customRender: 'operation'}
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        if (this.queryParam.auditTime) {
          params = {
            ...params,
            auditTimeStartDate: params.auditTime && params.auditTime[0].format('YYYY-MM-DD') + ' 00:00:00',
            auditTimeEndDate: params.auditTime && params.auditTime[1].format('YYYY-MM-DD') + ' 23:59:59'
          }
        }
        return this.$api.cultivate.courseList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yx: []
      },
      quoteVisible: false,
      quoteList: []
    }
  },
  components: {STable, courseQuote},
  computed: {
    ...mapState({
    })
  },
  created () {
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'kcssyxm', this.selectMenu.yx[0].dwh)
        }
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要课程信息')
        return
      }
      this.$confirm({
        title: '确定删除所选中的课程信息?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除',
        centered: true,
        onOk: () => {
          let ids = this.selectedRows.map(a => {
            return a.id
          })
          this.$api.cultivate.courseDelete(ids.join(',')).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => { }
      })
    },
    deleteCourse (c) {
      this.$confirm({
        title: '确定删除所选课程信息?',
        content: '当您点击确定按钮后，此课程信息将会被彻底删除',
        centered: true,
        onOk: () => {
          this.$api.cultivate.courseDelete(c.id).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => { }
      })
    },
    triggerCourse (c, zt) {
      this.$confirm({
        title: '提示',
        content: `确定${zt === '01' ? '启用' : '停用'}课程信息吗？`,
        centered: true,
        onOk: () => {
          this.$api.cultivate.courseStatusModify({kch: c.kch, kkzt: zt}).then(() => {
            this.$message.success('操作成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => { }
      })
    },
    showQuote (c) {
      this.quoteVisible = true
      this.$api.cultivate.courseQuoteList(c.kch).then(res => {
        this.quoteList = res.data
      })
    }
  }
}
</script>

<style>

</style>
